package controllers.managers
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	
	import utils.LogUtil;

	/**
	 * 调度管理器
	 * 所有的时钟调度都有此管理器管理
	 * 否则出现资源回收操作的时候出现时钟依然在跑
	 * @author kant
	 * @date 2012-11-10
	 **/	
	public class ScheduleManager
	{
		public static var NAME : String = "ScheduleManager";
		
		private var map:Object = new Object();
		
		private var logger:ILogger =  LogUtil.getInst().getLogger( NAME );
		
		public function ScheduleManager()
		{
		}
		
		/**
		 * 创建时间实例
		 **/
		public function createTimer(name:String, delay:Number, callback:Function):Timer
		{
			logger.info( "创建调度: " + name + " 延时: " + delay );
			
			var tempTimer : Timer  = null;
			if(map[name] == null)
			{
				tempTimer = new Timer(delay);
				tempTimer.addEventListener(TimerEvent.TIMER, callback);
				map[name]  = tempTimer;
			}
			else
			{
				tempTimer = map[name];
			}
			return tempTimer;
		}
		
		public function clearTasks():void
		{
			logger.info( "清除调度");
			for each(var item:Object in map)
			{
				var task:Timer = Timer(item);
				task.stop();
			}
		}
		
		/**
		 * 关闭指定的task
		 **/
		public function stopTask(name:String):void
		{
			if(map[name] != null)
			{
				logger.info( "关闭调度 :" + name);
				var task:Timer = Timer(map[name]);
				task.stop();
			}
		}
	}
}